from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from .neo4j_connection import graph
from .api_instance import api_instance


class ActionAskAboutPlace(Action):
    def name(self) -> Text:
        return "action_ask_about_place"

    async def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        place_name = next(tracker.get_latest_entity_values("place"), None)
        place_list = graph.query_place(place_name)

        if not place_name:
            api_instance.ask(tracker.events, dispatcher)
            return []

        if not place_list:
            dispatcher.utter_message("不好意思，我不清楚你问的" + (place_name if place_name else "") + "是哪里。", attachment="finish")
            return []

        place_node = place_list[0]["p"]
        message = (place_name
                   + ("位于" + place_node["location"] + "，" if "location" in place_node else "")
                   + "这里" + place_node["des"])

        api_instance.optimize_response(tracker.events, message, dispatcher)
        return []
